/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int dfs_help(TreeNode* root,int& maxS)
    {
        if(!root) return 0;
        int leftMax=dfs_help(root->left,maxS);
        int rightMax=dfs_help(root->right,maxS);
        int lmr=root->val+max(0,leftMax)+max(0,rightMax);
        int ret=root->val+max(0,max(leftMax,rightMax));
        maxS=max(maxS,max(lmr,ret));
        return ret;
    }
    int maxPathSum(TreeNode* root) {
        int maxS=INT_MIN;
        dfs_help(root,maxS);
        return maxS;
    }
};
